package cn.gov.pbc.gd.credit.web.controller.log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.liu.jbase.PageListVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.constant.ResCode;

import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.ExcelUtil;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.log.service.inquiry.ILogInquiryService;
import cn.gov.pbc.gd.credit.log.service.login.ILogLoginService;
import cn.gov.pbc.gd.credit.log.service.operate.ILogOperateService;
import cn.gov.pbc.gd.credit.user.service.corp.ICorpService;
import cn.gov.pbc.gd.credit.vo.corp.ResCorpVo;
import cn.gov.pbc.gd.credit.vo.log.LogInquiryVo;
import cn.gov.pbc.gd.credit.vo.log.LogLoginVo;
import cn.gov.pbc.gd.credit.vo.log.LogOperateVo;

@Controller
@RequestMapping("/log")
public class LogController {

	@Autowired
	private ILogLoginService logLoginService;
	
	@Autowired
	private ILogOperateService logOperateService;
	
	@Autowired
	private ILogInquiryService logInquiryService;
	
	@Autowired
	private ICorpService corpService;
	
	@Autowired
	private IRedisService redisService;
	 
	private List<LogInquiryVo> logInquiryVoList=new ArrayList<LogInquiryVo>();//保存查询日志数据的集合
	@RequestMapping("/login/page")
	public String loginPage(LogLoginVo vo, Model model, HttpServletRequest request){
		
		model.addAttribute("result", logLoginService.page(vo, request));
		
		return "log/login/page";
	}
	
	@RequestMapping("/operate/page")
	public String operatePage(LogOperateVo vo, Model model, HttpServletRequest request){
		
		model.addAttribute("result", logOperateService.page(vo, request));
		return "log/operate/page";
	}
	
	@RequestMapping("/inquiry/page")
	public String inquiryPage(LogInquiryVo vo, Model model, HttpServletRequest request) throws Exception{
		if(logInquiryVoList!=null&&logInquiryVoList.size()>0){
			logInquiryVoList.removeAll(logInquiryVoList);
		}
		ResultVo<List<LogInquiryVo>> resultVo=logInquiryService.getAll(vo);
		logInquiryVoList=resultVo.getData();
		UserModel userModel = UserUtil.getUserModel(vo.getSid(), redisService);
		ResultVo<List<ResCorpVo>> resultVoList = corpService.listById(userModel.getCorpId(), request);
		//根据分行ID，查询当前分行以及其下级分行
		List<ResCorpVo>resCorpVo=resultVoList.getData();
		//查询日志信息
		ResultVo<PageListVo<LogInquiryVo>>pageList=logInquiryService.page(vo, request);
		PageListVo<LogInquiryVo> pageListVo=pageList.getData();
		//根据corpId找到其对应的corpName
		int length=0;
		if(pageListVo.getCurrPage()==1){//当前页是第一页
			if(pageListVo.getTotal()>pageListVo.getPageSize()){
				length=pageListVo.getPageSize();
			}else{
				length=(int) pageListVo.getTotal();
			}
		}else{
			length=(int) (pageListVo.getTotal()-(pageListVo.getCurrPage()-1)*pageListVo.getPageSize());
		}
		for(int i=0;i<length;i++){
			LogInquiryVo logInquiryVo=(LogInquiryVo) pageListVo.getRows().get(i);
			for(int j=0;j<resCorpVo.size();j++){
				if(logInquiryVo.getCorpId().equals(resCorpVo.get(j).getId())){
					pageList.getData().getRows().get(i).setCorpId(resCorpVo.get(j).getName());
				}
			}
			
		}
		model.addAttribute("result", pageList);
		
		return "log/inquiry/page";
	}
	@RequestMapping(value="/exportData",produces = "application/json;charset=utf-8")
	public String exportData(LogInquiryVo logInquiryVo,HttpServletRequest request,Model model) throws IOException{
		ResultVo<List<LogInquiryVo>> resultVo=new ResultVo();
		JSONObject resulJson=new JSONObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		List<String> fileList=new ArrayList<String>();//存储文件夹名称的集合
		for(int w=0;w<logInquiryVoList.size();w++){
			Date createTime=logInquiryVoList.get(w).getCreateTime();
			String inquireTime=sdf.format(createTime);
			if(!fileList.contains(inquireTime+"/"+logInquiryVoList.get(w).getUserName())){
				fileList.add(inquireTime+"/"+logInquiryVoList.get(w).getUserName());
			}
		}
		
		String path="d:/";//导出数据的路径
		for(int v=0;v<fileList.size();v++){
			File folder = new File(path+fileList.get(v));
			if (!folder.exists()) {
				folder.mkdirs();
			}
		}
		List<LogInquiryVo>classfiedLogInquiryVoList=new ArrayList<LogInquiryVo>();//每个文件夹下的数据的集合
		for(int v=0;v<fileList.size();v++){
			for(int k=0;k<logInquiryVoList.size();k++){
				if(fileList.get(v).equals(sdf.format(logInquiryVoList.get(k).getCreateTime())+"/"+logInquiryVoList.get(k).getUserName())){
					classfiedLogInquiryVoList.add(logInquiryVoList.get(k));
				}
			}
			//导出数据的文件的名称
			String excelName=fileList.get(v).substring(0,fileList.get(v).lastIndexOf("/")).replace("/", ".")+fileList.get(v).substring(fileList.get(v).lastIndexOf("/")+1)+".xls";
			FileOutputStream os = new FileOutputStream(path+fileList.get(v)+"/"+excelName);
			//导出数据到excel
			byte[] bytes=ExcelUtil.exportDataToExcel(classfiedLogInquiryVoList, "查询日志数据");
			classfiedLogInquiryVoList.removeAll(classfiedLogInquiryVoList);
	        os.write(bytes);
	        os.flush();
	        os.close();
		}
		resulJson.put("success", true);
		resultVo.setCode(ResCode.SUCCESS);
		model.addAttribute("result", resulJson);
		return "log/inquiry/page";
		
	}
}
